package dev.android.yeqh.iask_sina;

import java.util.List;
import net.youmi.android.AdManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import dev.android.yeqh.iask_sina.adapter.keyListAdapter;
import dev.android.yeqh.iask_sina.common.Constants;
import dev.android.yeqh.iask_sina.common.NetTask;
import dev.android.yeqh.iask_sina.common.Utils;
import dev.android.yeqh.iask_sina.model.NetTaskParam;

public class IaskDictActivity extends BaseActivity {
	EditText edtKeyword = null;
	ListView lstKeywords = null;
	keyListAdapter adapter = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
        //第一个参数为您的应用发布Id
  		//第二个参数为您的应用密码
  		//第三个参数是请求广告的间隔，有效的设置值为30至200，单位为秒
  		//第四个参数是设置测试模式，设置为true时，可以获取测试广告，正式发布请设置此参数为false
  		AdManager.init(this, "c0ec71900ad2d601", "c2c14177fd5b6632", 30, false);
        
        edtKeyword = (EditText)findViewById(R.id.txt_keyword);
        lstKeywords = (ListView)findViewById(R.id.lst_keywords);
        
        new NetTask(this, false).execute(new NetTaskParam(Constants.TYPE_GET_WORDS, null));
    }
    
    public void search(View view) {
    	String key = edtKeyword.getText().toString();
    	if (Utils.empty(key)) {
    		edtKeyword.setError(getString(R.string.error_keyword_is_null));
    	} else {
    		Intent intent = new Intent(this, SearchResultActivity.class);
    		intent.putExtra("keyword", key);
    		startActivity(intent);
    	}
    }
    
    @SuppressWarnings("unchecked")
	@Override
    public void updateUI(Object result) {
    	try {
    		List<String> keywords = (List<String>)result;
    		adapter = new keyListAdapter(this, keywords, R.layout.item_keyword);
    		lstKeywords.setAdapter(adapter);
    	} catch (Exception e) {
			Utils.showMessage(this, e);
		}
    	super.updateUI(result);
    }
}